//
//  CHSDictionaryCrashController.m
//  CAIAvoidCrash_Example
//
//  Created by 朱新明 on 2023/9/4.
//  Copyright © 2023 海参. All rights reserved.
//

#import "CHSDictionaryCrashController.h"

@interface CHSDictionaryCrashController ()
@property (nonatomic, strong) NSMutableArray *dataMArray0;
@property (nonatomic, strong) NSMutableArray *dataMArray1;

@end

@implementation CHSDictionaryCrashController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setUI];
    [self setData];
}
- (void)setUI{
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    self.navigationItem.title = @"NSDictionary/NSMutableDictionary";
}

#pragma mark - Data

- (void)setData
{
    NSArray *arr = @[
        @{@"0":@"字面值创建@{}(这种创建方式底层就是调用2中的方法)"},
        @{@"1":@"【- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects forKeys:(const KeyType <NSCopying> _Nonnull [_Nullable])keys count:(NSUInteger)cnt;/返回过滤nil（key/value）后的有效字典或空字典__NSDictionary0"},
        @{@"2":@"+ (instancetype)dictionaryWithObjects:(const ObjectType _Nonnull [_Nullable])objects forKeys:(const KeyType <NSCopying> _Nonnull [_Nullable])keys count:(NSUInteger)cnt;/调用2中的方法/返回过滤nil（key/value）后的有效字典或空字典__NSDictionary0"},
        @{@"3":@"【+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType <NSCopying>)key;//调用2中的方法/返回过滤nil（key/value）后的有效字典或空字典__NSDictionary0"},
        @{@"4":@"【- (instancetype)initWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;//返回nil"},
    ];
    self.dataMArray0 = [NSMutableArray arrayWithArray:arr];
    
    
    NSArray *arr1 = @[
        @{@"0":@"- (void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey;/obj不能为空&key cannot be nil"},
        @{@"1":@"- (void)setObject:(nullable ObjectType)obj forKeyedSubscript:(KeyType <NSCopying>)key;//使用下标方式添加key-value/obj可以为空 &key cannot be nil"},
        @{@"2":@" - (void)removeObjectForKey:(KeyType)aKey;/key cannot be nil"},

    ];
    self.dataMArray1 = [NSMutableArray arrayWithArray:arr1];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.dataMArray0.count;
    }
    return self.dataMArray1.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.numberOfLines = 0;
     
    if (indexPath.section == 0) {
        NSDictionary *dic = [self.dataMArray0 objectAtIndex:indexPath.row];
        NSString *value = [[dic allValues] firstObject];
        cell.textLabel.text =  value;
    }
    else if (indexPath.section == 1) {
        NSDictionary *dic = [self.dataMArray1 objectAtIndex:indexPath.row];
        NSString *value = [[dic allValues] firstObject];
        cell.textLabel.text =  value;
    }
    else{

    }
    // Configure the cell...
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *title = nil;
    switch (section) {
        case 0:
            title = @"NSDictionary初始化方法";
            break;
        case 1:
            title = @"NSMutableDictionary方法";
            break;
        default:
            break;
    }
    return title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        NSDictionary *dic = [self.dataMArray0 objectAtIndex:indexPath.row];
        NSString *key = [[dic allKeys] firstObject];
        if ([key isEqualToString:@"0"]) {
            [self crashInit1];
        }
        else if ([key isEqualToString:@"1"]) {
            [self crashInit2];
        }
        else if ([key isEqualToString:@"2"]) {
            [self crashInit3];
        }
        else if ([key isEqualToString:@"3"]) {
            [self crashInit4];

        }
        else if ([key isEqualToString:@"4"]) {
            [self crashInit5];

        }
    }
    if (indexPath.section == 1) {
        NSDictionary *dic = [self.dataMArray1 objectAtIndex:indexPath.row];
        NSString *key = [[dic allKeys] firstObject];
        if ([key isEqualToString:@"0"]) {
            [self crashSetObjectForKey];
        }
        else if ([key isEqualToString:@"1"]) {
            [self crashSetObjectForKeyedSubscript];
        }
        else if ([key isEqualToString:@"2"]) {
            [self crashRemoveObjectForKey];
        }
        else if ([key isEqualToString:@"3"]) {

        }
       
    }
}


- (void)crashInit1{
    id someValue = nil;
    NSDictionary *dic = @{@"1":someValue};
}

- (void)crashInit2{
    id someObject = nil;
    id someValue = nil;
    id objects[] = {someObject, @"Hello, World!", @42, someValue };
    id keys[] = {@"anObject", @"helloString", @"magicNumber", @"aValue" };
    NSUInteger count = sizeof(objects) / sizeof(id);
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys count:count];
}


- (void)crashInit3{
    id someObject = nil;
    id someValue = nil;
    id objects[] = {someObject, @"Hello, World!", @42, someValue };
    id keys[] = {@"anObject", @"helloString", @"magicNumber", @"aValue" };
    NSUInteger count = sizeof(objects) / sizeof(id);
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count];
}

- (void)crashInit4{
    id someObject = nil;
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:someObject forKey:@"1"];
}

- (void)crashInit5{
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:@[] forKeys:@[@"1"]];
}

- (void)crashSetObjectForKey{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"1" forKey:@"key"];
    id obj = nil;
    [dic setObject:obj forKey:@"1"];
}

- (void)crashSetObjectForKeyedSubscript{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"1" forKey:@"key"];
    id obj = nil;
//    dic[@"key2"] = obj;
    [dic setObject:obj forKeyedSubscript:nil];
}

- (void)crashRemoveObjectForKey{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"1" forKey:@"key"];
    id key = nil;
    [dic removeObjectForKey:key];
}

///





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
